package arch.dao.impl.user;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import arch.dao.impl.user.mapper.RoleMapper;
import arch.dao.user.RoleDao;
import arch.domain.user.Role;
/**
 * 
 * @author ZhaoWangLong Sep 4, 2012
 *
 */
@Repository("roleDao")
public class RoleImpl implements RoleDao {
	@Resource private JdbcTemplate jdbcTemplate;
	private final Logger logger = Logger.getLogger(RoleImpl.class);
	
	@SuppressWarnings("unchecked")
	public List<Role> queryList(String sql, Object... objects) {
		logger.debug("SQL > "+sql);
		
		return this.jdbcTemplate.query(sql, objects, new RoleMapper());
	}

	public Role find(Serializable primaryKey) {
		String sql = "select ROLE_ID, ROLE_NAME, DESCRIPTION, CREATE_TIME, "
			+" LAST_UPDATE_TIME  from ID_ROLE where ROLE_ID = ? ";
		try {
			return (Role)this.jdbcTemplate.queryForObject(sql, new Object[]{primaryKey}, new RoleMapper());
		}catch (EmptyResultDataAccessException e) {
			return null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void merge(Role entity) {
		String sql = "update ID_ROLE set ROLE_NAME = ? , DESCRIPTION = ? , LAST_UPDATE_TIME = ? "
			+" where ROLE_ID = ? ";
		
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getRoleName(),
				entity.getDescription(),
				entity.getLastUpdateTime(),
				entity.getRoleId()
		});
	}

	public void persist(Role entity) {
		String sql = "insert into ID_ROLE (ROLE_ID, ROLE_NAME, DESCRIPTION, CREATE_TIME,"
			+" LAST_UPDATE_TIME) values (?,?,?,?,?) ";

		this.jdbcTemplate.update(sql,new Object[]{
				entity.getRoleId(),
				entity.getRoleName(),
				entity.getDescription(),
				entity.getCreateTime(),
				entity.getLastUpdateTime()
		});
	}

	public void remove(Serializable primaryKey) {
		String sql = "delete from ID_ROLE where ROLE_ID = ? ";
		
		this.jdbcTemplate.update(sql,new Object[]{primaryKey});
	}

}
